/**
 * Copyright (c) 时间, Tal.Ben 袁东昊 (ydonghao2@gmail.com).
 *
 * 版权
 */
package com.talben.soudian.fragment;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.talben.soudian.AppContext;
import com.talben.soudian.R;
import com.talben.soudian.TDevice;
import com.talben.soudian.api.SouDianAPI;
import com.talben.soudian.base.BaseFragment;
import com.talben.soudian.base.BaseListFragment;
import com.talben.soudian.bean.Gift;
import com.talben.soudian.bean.User;
import com.talben.soudian.ui.ChangeUserActivity;
import com.talben.soudian.ui.LoginActivity;
import com.talben.soudian.ui.OnTabReselectListener;
import com.talben.soudian.ui.UIHelper;

/**
 * @Title: GiftGotFragment.java
 * @Package com.talben.soudian.fragment
 * @Description: TODO(添加描述)
 * @author Tal.Ben
 * @date 2015年6月6日 下午6:37:39
 * @version v1.0
 */
public class GiftGotFragment extends BaseFragment{

	@InjectView(R.id.tv_show_gift_log)
	TextView mTvShowGiftLog;
	@InjectView(R.id.btn_login)
	Button mBtnLogin;
	
	@Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_got_gift,
                container, false);
        ButterKnife.inject(this, view);
        initView(view);
        return view;
    }
	
	@Override
    public void initView(View view) {
		if (!TDevice.isNetworkReady(getActivity())) {
            AppContext.showToastShort(R.string.tip_no_internet);
            mTvShowGiftLog.setText("您没有连接网络");
            return;
        } else {
        	if (AppContext.getInstance().isLogin()) {
        		SouDianAPI.getGiftLog(mHandler);
    		} else {
    			mTvShowGiftLog.setText("您没有登录");
    			mBtnLogin.setVisibility(View.VISIBLE);
    		}
        }
		
		mBtnLogin.setOnClickListener(this);
    }
	
	private final AsyncHttpResponseHandler mHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onFailure(int arg0, Header[] arg1, byte[] arg2,
				Throwable arg3) {
			hideWaitDialog();
			AppContext.showToast(R.string.getting_score_error);
		}

		@Override
		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
			// 处理成功后的动作
			try {
				//处理Json
                String result = new String(arg2,"utf-8");
                JSONObject resJObj = new JSONObject(result);
                JSONArray jArray = resJObj.getJSONArray("rows");
                String data = "";
        		for (int j = 0; j < jArray.length(); j++) {
        			JSONObject json = jArray.getJSONObject(j);
        			data += json.getString("text")+"\n";
        		}
        		mTvShowGiftLog.setText(data);
			} catch (Exception e) {
				e.printStackTrace();
                onFailure(arg0, arg1, arg2, e);
			}
		}
    };
	
	@Override
    public void onClick(View v) {
        final int id = v.getId();
        switch (id) {
        case R.id.btn_login:
            UIHelper.gotoLoginPage(getActivity());
            break;
        default:
            break;
        }
    }
}

